<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员发票类
 * @package  module/base/customer
 * @author   allen.qiang
 * @version 1.0
 */

class custInvoice {

    /**
     * 查询发票信息
     * @param array $arg_array 查询参数数组
     * @return null
     */
    public function searchCustInvoice( &$arg_array ) {
        
        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //会员号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //发票ID
        if (isset($arg_array['invoiceID']) && is_numeric($arg_array['invoiceID'])) {
            $sql .= ' AND invoiceID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['invoiceID'];
        }

        //发票title
        if (isset($arg_array['invoice_title'])) {
            $sql .= ' AND invoice_title = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['invoice_title'];
        }

        //发票
        if (isset($arg_array['invoice_title_type']) && is_numeric($arg_array['invoice_title_type'])) {
            $sql .= ' AND invoice_title_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['invoice_title_type'];
        }


        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_invoice ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM cust_invoice ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'pcustID' => $item->pcustID,
                    'invoiceID' => $item->invoiceID,
                    'invoice_type' => $item->invoice_type,
                    'invoice_title_type' => $item->invoice_title_type,
                    'invoice_title' => $item->invoice_title
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 添加/修改会员发票信息
     * @param array $arg_array 发票参数
     * @return array
     * @throws Exception
     */
    public function editCustInvoice( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['invoice_type'], $arg_array['invoice_title_type'], $arg_array['invoice_title'], $arg_array['pcustID'], $arg_array['creator']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (!in_array($arg_array['invoice_type'], array(1, 2)))
            return array( 'code' => 400, 'message' => '请选择发票类型' );

        if (!in_array($arg_array['invoice_title_type'], array(1, 2)))
            return array( 'code' => 400, 'message' => '请选择发票抬头类型' );

        if($arg_array['invoice_title_type'] != 1 && empty($arg_array['invoice_title'])){
            return array( 'code' => 400, 'message' => '发票抬头不能为空，请填写' );
        }

        if (!isset($arg_array['invoiceID']) || !is_numeric($arg_array['invoiceID']) || $arg_array['invoiceID'] < 0)
            $arg_array['invoiceID'] = 0;

        quanli_require('module/base/common/quanliCommon.class.php');

        try {

            $invoice_obj = new entity('cust_invoice');
            $invoice_obj->pcustID = $arg_array['pcustID'];
            if ($arg_array['invoiceID'] > 0) {
                $invoice_obj->invoiceID = $arg_array['invoiceID'];
                if (!$G_SHOP->DBCA->fetch($invoice_obj))
                    throw new Exception('发票信息没有找到，修改失败', 404);
                $invoice_obj->editor = $arg_array['creator'];
            } else {
                $invoice_obj->invoiceID = generate_seqno('seqno_invoice');
                $invoice_obj->creator = $arg_array['creator'];
            }
            $invoice_obj->invoice_type = $arg_array['invoice_type'];
            $invoice_obj->invoice_title_type = $arg_array['invoice_title_type'];
            $invoice_obj->invoice_title = convertVar( $arg_array['invoice_title'] );
            $G_SHOP->DBCA->save($invoice_obj);

            $invoice_array = array(
                'invoiceID' => $invoice_obj->invoiceID,
                'invoice_type' => $invoice_obj->invoice_type,
                'invoice_title_type' => $invoice_obj->invoice_title_type,
                'invoice_title' => $invoice_obj->invoice_title ? $invoice_obj->invoice_title : ''
            );


            return array( 'code' => 200, 'message' => 'ok', 'data' => $invoice_array );


        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 删除发票信息
     * @param int $pcustID    会员号
     * @param int $invoiceID  发票号
     * @return array
     * @throws Exception
     */
    public function delCustInvoice($pcustID, $invoiceID) {

        global $G_SHOP;

        if (!isset($pcustID, $invoiceID) || !is_numeric($pcustID) || !is_numeric($invoiceID))
            return array( 'code' => 400, 'message' => '缺少参数' );

        try {

            //查询会员发票信息
            $invoice_obj = new entity('cust_invoice');
            $invoice_obj->pcustID = $pcustID;
            $invoice_obj->invoiceID = $invoiceID;
            if (!$G_SHOP->DBCA->fetch($invoice_obj))
                throw new Exception('发票信息没有找到', 404);

            //删除发票
            $G_SHOP->DBCA->delete($invoice_obj);

            return array( 'code' => 200, 'message' => 'ok' );


        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

}